package com.woniu.controller;

import com.woniu.entity.RbacManager;
import com.woniu.entity.RbacManagerDTO;
import com.woniu.entity.ResponseEntity;
import com.woniu.service.RbacManagerService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author WenXin
 * @version 1.0
 * @description: TODO
 * @date 2022/9/1 15:03
 */
@RestController
@RequestMapping("/rbacManager")
@CrossOrigin
public class RbacManagerController {
    @Autowired
    RbacManagerService service;
    
    //查询所有用户信息
    @RequiresRoles(logical = Logical.OR, value = {"admin", "normal"})//角色为指定值才可以进入findAll方法
    @GetMapping("findAll/{pageNum}/{pageSize}")
    public ResponseEntity findAll(@PathVariable int pageNum, @PathVariable int pageSize) {

        return service.findAll(pageNum, pageSize);
    }

    //删除某行的代码
    @RequestMapping("delete")
    public ResponseEntity deleteById(int id) {
        int delete = service.deleteOne(id);
        if (delete > 0) {
            return new ResponseEntity(200, "删除id为:" + id + "的用户成功！", null);
        } else {
            return new ResponseEntity(500, "删除失败", null);
        }


    }

    //删除某行的代码
    @RequestMapping("update")
    public ResponseEntity updateById(@RequestBody RbacManager rbacManager) {
        if (rbacManager.getId() == 0) {
            return new ResponseEntity(500, "修改的用户不存在或id不能为空", null);
        } else {
            int count = service.findCount(rbacManager);
            RbacManager one = service.findOne(rbacManager.getId());
            if (count > 0 && !one.getAccount().equals(rbacManager.getAccount())) {
                return new ResponseEntity(500, "已存在其他同名账户无法修改", null);
            }
            int i = service.updateOne(rbacManager);
            if (i > 0) {
                return new ResponseEntity(200, "更新成功:" + rbacManager.getAccount() + "的用户成功！", null);
            } else {
                return new ResponseEntity(500, "未知错误更新失败", null);
            }
        }


    }

    //删除某行的代码
    @RequestMapping("insert")
    public ResponseEntity insertById(@RequestBody RbacManagerDTO rbacManagerDTO) {

        int count = service.findCount(rbacManagerDTO);
        if (count > 0) {
            return new ResponseEntity(500, "新增失败账号已存在或其他原因", null);
        }
        int insert = service.addOne(rbacManagerDTO);
        if (insert > 0) {
            return new ResponseEntity(200, "新增用户名为:" + rbacManagerDTO.getAccount() + "的用户成功！", null);
        } else {
            return new ResponseEntity(500, "新增失败账号已存在或其他原因", null);
        }

    }

    //使用restful风格的模糊搜索功能

    @PutMapping("/{pageNum}/{pageSize}")
    public ResponseEntity findByrbacManagerDTO(@RequestBody RbacManagerDTO rbacManagerDTO, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return service.findByrbacManagerDTO(rbacManagerDTO, pageNum, pageSize);
    }

    //用户状态修改的方法
    @GetMapping("/editStatus/{id}")
    public ResponseEntity editStatus(@PathVariable int id) {
        return service.editStatus(id);
    }

    //重置密码的方法
    @GetMapping("/initPwd/{id}")
    public ResponseEntity initPwd(@PathVariable int id) {
        return service.initPwd(id);
    }

    //编辑方法
    @PostMapping("/updateRole")
    public ResponseEntity updateRole(@RequestBody RbacManagerDTO rbacManagerDTO) {

        return service.updateRole(rbacManagerDTO);
    }
}
